//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  updatePetCell.m
//  dingding
//
//  Created by LHL on 14/12/15.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "updatePetCell.h"
#import "UserModel.h"
#pragma mark -

@interface updatePetCell()
{
	//<#@private var#>
}
@end

@implementation updatePetCell{
    NSString *imgstr;
    NSString *birth;
    BOOL ischange;
}

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
DEF_OUTLET(BeeUIButton, female)
DEF_OUTLET(BeeUIButton, male)
DEF_OUTLET(BeeUIButton, petAge)
DEF_OUTLET(BeeUIButton, addPhoto)
DEF_OUTLET(BeeUIButton, petType)
DEF_OUTLET(BeeUILabel, petAgeLabel)
DEF_OUTLET(BeeUILabel, hintHead)
DEF_OUTLET(BeeUITextField, petSign)
DEF_OUTLET(BeeUITextField, petName)
DEF_OUTLET(BeeUILabel, petTypeLabel)
DEF_OUTLET(BeeUILabel, maleLabel)
DEF_OUTLET(BeeUILabel, femaleLabel)

- (void)load
{
}

- (void)unload
{
}

- (void)dataDidChanged
{
    self.female.adjustsImageWhenHighlighted = NO;
    self.male.adjustsImageWhenHighlighted = NO;
    if (self.data!= nil) {
//        PETINFOMODEL *model = self.data[0];
        NSString *name = self.data;
        if (name!=nil && ischange == NO) {
//            SDWebImageManager *manager = [SDWebImageManager sharedManager];
//            [manager downloadWithURL:[NSURL URLWithString:model.avatar] options:SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize,NSInteger expectedSize){
//                
//            }completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){
//                if (![imgstr isEqualToString:model.avatar]) {
//                    imgstr = model.avatar;
//                    [[NSUserDefaults standardUserDefaults]setObject:UIImageJPEGRepresentation(image, 1.0) forKey:@"head_img"];
//                    [[NSUserDefaults standardUserDefaults]synchronize];
//                    [self.addPhoto setBackgroundImage:image forState:0];
//                }
//            }];
            [self.addPhoto setBackgroundImage:[UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"head_img"]]forState:0];
            [self.addPhoto setBackgroundImage:[UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"head_img"]]forState:UIControlStateHighlighted];
            self.petName.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"petname"];
            self.petSign.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"signature"];;
            self.petTypeLabel.text = name;
            if (birth == nil) {
                NSDate *now = [NSDate date];
                NSTimeZone *GMT = [NSTimeZone systemTimeZone];
                [NSTimeZone setDefaultTimeZone:GMT];
                NSInteger interval = [GMT secondsFromGMTForDate:now];
                NSDate *localdate = [now dateByAddingTimeInterval:interval];
                
                NSCalendar *cal = [NSCalendar currentCalendar];
                unsigned int unitFlag;
                if (IOS8_OR_LATER) {
                    unitFlag = NSCalendarUnitMonth;
                }else{
                    unitFlag  = NSDayCalendarUnit;
                }
                NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
                [formatter setLocale:[NSLocale currentLocale]];
                [formatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *date = [formatter dateFromString:[[NSUserDefaults standardUserDefaults] objectForKey:@"birthday"]];
                birth = [[NSUserDefaults standardUserDefaults] objectForKey:@"birthday"];
                NSDateComponents *day = [cal components:unitFlag fromDate:date toDate:localdate options:0];
                NSInteger distanceMonth = [day month];
                if (distanceMonth>=12) {
                    self.petAgeLabel.text = [NSString stringWithFormat:@"%d岁%d个月",distanceMonth/12,distanceMonth%12];
                }else{
                    self.petAgeLabel.text = [NSString stringWithFormat:@"%ld个月",(long)distanceMonth];
                }
            }
            ischange = YES;
            NSString *gender = [[NSUserDefaults standardUserDefaults]objectForKey:@"gender"];
            if ([gender isEqualToString:@"female"]) {
                self.female.selected = YES;
            }else
            {
                self.male.selected = YES;
            }
        }
    }

}

- (void)layoutDidFinish
{
    // TODO: custom layout here
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    for (BeeUITextField *t  in self.subviews) {
        [t resignFirstResponder];
    }
}
ON_SIGNAL3( BeeUITextField, RETURN, signal ){
    UITextField *input = (UITextField *)signal.source;
    [input resignFirstResponder];
}

ON_SIGNAL3(BeeUIButton, male, SIGNAL){
    if (self.female.selected) {
        self.female.selected = NO;
    }
    self.male.selected = !self.male.selected;
}

ON_SIGNAL3(BeeUIButton, female, SIGNAL){
    if (self.male.selected) {
        self.male.selected = NO;
    }
    self.female.selected = !self.female.selected;
}
@end
